package com.hzit.controller;

import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.entity.Student;
import com.hzit.service.StudentService;
import com.hzit.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.channels.InterruptedByTimeoutException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@RestController
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private StudentService studentService;

	@GetMapping("/list")
	public R list(){
		List<StudentVo> studentList = studentService.findAll();
		return R.ok().put("students",studentList);
	}
	//分页查询
	@GetMapping("/findByPage")
	public R findByPage(@RequestParam(value="page",defaultValue = "1") Integer page,
	                    @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize
	                    ){
		PageResult<StudentVo> pr = studentService.findByPage(page,pageSize);
		return R.ok().put("pr",pr);
	}
	// 条件查询带分页
	@PostMapping("/search")
	public R search(@RequestParam(value="page",defaultValue = "1") Integer page,
	                @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize,
	                @RequestBody StudentVo vo){
		PageResult<StudentVo> pr = studentService.search(page,pageSize,vo);
		return R.ok().put("pr",pr);
	}
	// 添加学生
	@PostMapping("/add")
	public R add(@RequestBody StudentVo vo){
		studentService.add(vo);
		return R.ok("添加学生成功！");
	}
	// 修改学生
	@PostMapping("/update")
	public R update(@RequestBody StudentVo vo){
		studentService.update(vo);
		return R.ok("修改学生成功！");
	}

	// 删除学生
	@GetMapping("/delete")
	public R delete(@RequestParam("sids") Integer[] sids){
		studentService.delete(sids);
		return R.ok("删除学生成功！");
	}
}
